import BlueTooth.BTClient;


//plan 20091231:
//   implement MainMenu use list temp
//
//
public class GameManage
{
  static public Protocol  protocolProxy ;
	   
  
	GameEngine m_gameEngine;
	GameManage(GameEngine gameEngine)
	{
		m_gameEngine = gameEngine;
	}
//game main logic and game data

  static int s_iGameState; //state of the game
  static int s_iPreGameState;
  
  public int s_gradeId;
//0: normal Human&Computer ; 1:local network Human&human
 // 2:buletooth Human&Human; 3:internet Human&Human
  static public int s_iGameModeState =0;
  static public int s_iGameIsRun = 0;
  static public Sound s_sound ;
  public static ImageManage s_imageManage = new ImageManage();
  
  
  public static boolean firstTimeInit =false;
  static public int s_iGameOverState =0; // if game over?
  public void gameUpdate()
   {
	 	try
	 	{
	 		switch(s_iGameState)
	 		{
	 		case DEF.GS_EXIT:
	 			m_gameEngine.stop() ;
	 			break;
	 		case DEF.GS_INIT:
	 			//only for test now
	 			///setGameState(DEF.GS_LOCALNET_BATTLE);
	 			s_sound = new Sound(m_gameEngine);
	 			s_sound.loadSound();
	 			s_sound.setSound(true);
	 			s_sound.soundPlay(DEF.M_TITLE_SOUND);
	 			
	 			s_imageManage.setMenuBg(s_imageManage.createNewImage(DEF.IMAGE_BG_ITEMS_LIST[DEF.MENU_BG_IMAGE]));
	 			setGameState(DEF.GS_MAIN_MENU);
	 			
	 			Menu.setMenuItems( DEF.MAIN_MENU_ITEMS_LIST);
	 			break;
	 		case DEF.GS_NEWGAME_MENU:
	 			
	 			//process press key
	 			if(GameInputKey.keyIsPressed(DEF.MOVE_UP))
	 			{
	 				if(Menu.getCurrentMenuIndex()== 0)  
	 					Menu.setCurrentMenuIndex( Menu.getCurrentMenuCount() -1);
	 				else	
	 					Menu.setCurrentMenuIndex( (Menu.getCurrentMenuIndex() -1)% Menu.getCurrentMenuCount());
	 			}
	 			else if(GameInputKey.keyIsPressed(DEF.MOVE_DOWN))
	 			{
	 				Menu.setCurrentMenuIndex((Menu.getCurrentMenuIndex() +1)% Menu.getCurrentMenuCount());
	 			}

	 			else if(GameInputKey.keyIsPressed(DEF.SELECT))
	 			{
	 				 s_gradeId =Menu.getCurrentMenuIndex();
	 				if(s_gradeId == 2)//hard
	 					s_gradeId =0 ;
	 				else if(s_gradeId == 1)//middle
	 					;
	 				else //easy
	 					s_gradeId = 10;
	 					
	 				m_gameEngine.m_aiGoBang.setAiGrade(s_gradeId);
	 				setGameState(DEF.GS_NORMAL_BATTLE);//new game

	 			}
	 			else if(GameInputKey.keyIsPressed(DEF.BACK))
	 			{
		 			setGameState(DEF.GS_MAIN_MENU);
		 			Menu.setMenuItems( DEF.MAIN_MENU_ITEMS_LIST);
	 			}
	 			
	 			break;
	 		case DEF.GS_NORMAL_BATTLE:	
	 			//init game start
	 			s_iGameModeState = 0;
	 			m_gameEngine.m_aiGoBang.InitializeBoard();
	 			
	 			int randBgId =0;///,i;
//	 			 while(true)
//	 			 {
	 			  randBgId = m_gameEngine.m_aiGoBang.rand(0,DEF.IMAGE_BG_ITEMS_LIST.length -1);
	 		//	  System.out.println(randBgId+"\n");
//	 			   if(randBgId >4)
//	 			   {
//	 			 randBgId = randBgId % (DEF.IMAGE_BG_ITEMS_LIST.length -1);
//	 				  break;
//	 			   }
//	 			 }
	 			s_imageManage.setInGameBg(s_imageManage.createNewImage(DEF.IMAGE_BG_ITEMS_LIST[DEF.IN_GAME_BG_START_IMAGE + randBgId]));
	 			s_iGameIsRun = 1;//start game
	 			s_iGameOverState = 0;///not  game over 
	 			s_sound.soundStop(); //stop the title music
	 			setGameState(DEF.GS_RUN);
	 			break;
	 		case DEF.GS_MAIN_MENU:
	 			//process press key
	 			if(GameInputKey.keyIsPressed(DEF.MOVE_UP))
	 			{
	 				if(Menu.currentMenuIndex== 0)
	 					Menu.currentMenuIndex = Menu.currentMenuCount -1;
	 				else	
	 				   Menu.currentMenuIndex =(--Menu.currentMenuIndex )% Menu.currentMenuCount;
	 			}
	 			else if(GameInputKey.keyIsPressed(DEF.MOVE_DOWN))
	 			{
	 				    Menu.currentMenuIndex =(++Menu.currentMenuIndex )% Menu.currentMenuCount;
	 			}
	 			else if(GameInputKey.keyIsPressed(DEF.MOVE_LEFT))
	 			{
	 				
	 			}
	 			else if(GameInputKey.keyIsPressed(DEF.MOVE_RIGHT))
	 			{
	 				
	 			}
	 			else if(GameInputKey.keyIsPressed(DEF.SELECT))
	 			{
	 				int itemId =Menu.getCurrentMenuIndex();
	 				if(itemId ==5) //exit game
	 				{
	 					setGameState(DEF.GS_EXIT);
	 					m_gameEngine.stoped = true;// Exit game
	 				}
	 				else if(itemId == 0)//new game
	 				{
	 					setGameState(DEF.GS_NEWGAME_MENU);//setGameState(DEF.GS_NORMAL_BATTLE);
	 				   Menu.setMenuItems( DEF.NEW_GAME_MENU_ITEMS_LIST);
	 				}
	 				else if(itemId ==2)
	 				{
	 					setGameState(DEF.GS_OPTIONS_MENU);//setGameState(DEF.GS_NORMAL_BATTLE);
		 				Menu.setMenuItemsWithTitle( DEF.MENU_TITLES_OPTIONS_LIST)	;//option ,set
	 				}
	 				else if(itemId ==1)
	 				{
	 					setGameState(DEF.GS_LOCALNET_MENU);//local battle
	 					Menu.setMenuItems( DEF.LOCALNET_MENU_ITEMS_LIST);
	 				}
	 				else if(itemId ==3)
	 				{
	 					setGameState(DEF.GS_HELP_MENU);//help menu
	 					Menu.setMenuItems( DEF.GS_HELP_MENU_ITEMS_LIST);//help battle
	 				}
	 				else if(itemId ==4)
	 				{
	 					setGameState(DEF.GS_ABOUT); //credit
	 					Menu.setMenuItems( DEF.GS_ABOUT_MENU_ITEMS_LIST);
	 				}
	 				
	 			}
	 			break;	
	 		case DEF.GS_OPTIONS_MENU:
	 			//process press key
	 			if(GameInputKey.keyIsPressed(DEF.MOVE_UP))
	 			{
	 				if(Menu.currentMenuIndex== 0)
	 					Menu.currentMenuIndex = Menu.currentMenuCount -1;
	 				else	
	 				   Menu.currentMenuIndex =(--Menu.currentMenuIndex )% Menu.currentMenuCount;
	 			}
	 			else if(GameInputKey.keyIsPressed(DEF.MOVE_DOWN))
	 			{
	 				    Menu.currentMenuIndex =(++Menu.currentMenuIndex )% Menu.currentMenuCount;
	 			}
	 			else if(GameInputKey.keyIsPressed(DEF.MOVE_LEFT))
	 			{
	 				
	 			}
	 			else if(GameInputKey.keyIsPressed(DEF.MOVE_RIGHT))
	 			{
	 				
	 			}
	 			else if(GameInputKey.keyIsPressed(DEF.SELECT))
	 			{
	 				int itemId =Menu.getCurrentMenuIndex();
	 				
	 				if(itemId == 0)//sound
	 				{
	 					s_sound.setSound();//on & off
	 					if(s_sound.getSoundStatus())
	 					s_sound.soundPlay(DEF.M_TITLE_SOUND);
	 					else
	 					s_sound.soundStop();
	 				}
	 				else if(itemId ==1)
	 				{
	 				  ; 
	 				}

	 				
	 			}
	 			else  if(GameInputKey.keyIsPressed(DEF.BACK))
		 		{
			 			setGameState(DEF.GS_MAIN_MENU);
			 			Menu.setMenuItems( DEF.MAIN_MENU_ITEMS_LIST);
		 		}
	 			break;	
	 		case DEF.GS_LOCALNET_MENU:
	 			//process press key
	 			if(GameInputKey.keyIsPressed(DEF.MOVE_UP))
	 			{
	 				if(Menu.getCurrentMenuIndex()== 0)
	 					Menu.setCurrentMenuIndex( Menu.getCurrentMenuCount() -1);
	 				else	
	 					Menu.setCurrentMenuIndex( (Menu.getCurrentMenuIndex() -1)% Menu.getCurrentMenuCount());
	 			}
	 			else if(GameInputKey.keyIsPressed(DEF.MOVE_DOWN))
	 			{
	 				Menu.setCurrentMenuIndex((Menu.getCurrentMenuIndex() +1)% Menu.getCurrentMenuCount());
	 			}

	 			else if(GameInputKey.keyIsPressed(DEF.SELECT))
	 			{
	 				if(Menu.getCurrentMenuIndex()==0)
	 				{
	 					setGameState(DEF.GS_BLUETOOTH_MENU);//bluetooth battle
	 					Menu.setMenuItems( DEF.BLUETOOTH_MENU_ITEMS_LIST);
	 				}
	 			}
	 			else if(GameInputKey.keyIsPressed(DEF.BACK))
	 			{
		 			setGameState(DEF.GS_MAIN_MENU);
		 			Menu.setMenuItems( DEF.MAIN_MENU_ITEMS_LIST);
	 			}
	 			break;
	 		case DEF.GS_BLUETOOTH_MENU:
	 			//process press key
	 			if(GameInputKey.keyIsPressed(DEF.MOVE_UP))
	 			{
	 				if(Menu.getCurrentMenuIndex()== 0)
	 					Menu.setCurrentMenuIndex( Menu.getCurrentMenuCount() -1);
	 				else	
	 					Menu.setCurrentMenuIndex( (Menu.getCurrentMenuIndex() -1)% Menu.getCurrentMenuCount());
	 			}
	 			else if(GameInputKey.keyIsPressed(DEF.MOVE_DOWN))
	 			{
	 				Menu.setCurrentMenuIndex((Menu.getCurrentMenuIndex() +1)% Menu.getCurrentMenuCount());
	 			}

	 			else if(GameInputKey.keyIsPressed(DEF.SELECT))
	 			{
	 				protocolProxy = new Protocol();
	 				if(Menu.getCurrentMenuIndex()==0)
	 				{
			 			s_iGameModeState = 2;
			 			m_gameEngine.m_aiGoBang.InitializeBoard();
			 			//init protocol server
			 			protocolProxy.initProtocol(Protocol.BLUETOOTH_PROTOCOL, true);
			 			s_sound.soundStop(); //stop the title music
		 				setGameState(DEF.GS_RUN);  
		 				s_iGameIsRun = 1;//start game
		 				s_iGameOverState = 0;///not  game over 
	 				}
	 				else if(Menu.getCurrentMenuIndex()==1)
	 				{
	 					//use client socket
			 			s_iGameModeState = 2;
			 			///m_gameEngine.m_aiGoBang.InitializeBoard();
			 			///init protocol client
			 			protocolProxy.initProtocol(Protocol.BLUETOOTH_PROTOCOL, false);
			 			///s_sound.soundStop(); //stop the title music
		 				///setGameState(DEF.GS_RUN);
		 				///s_iGameIsRun = 1;//start game
		 				///s_iGameOverState = 0;///not  game over
			 			Menu.setMenuItems(null);
			 			setGameState(DEF.GS_SERVER_LIST_MENU);
			 			firstTimeInit = true;
	 				}
	 				
	 				
	 				///s_imageManage.setInGameBg(s_imageManage.createNewImage(DEF.IMAGE_BG_ITEMS_LIST[DEF.IN_GAME_BG_START_IMAGE]));
	 			}
	 			else if(GameInputKey.keyIsPressed(DEF.BACK))
	 			{
 					setGameState(DEF.GS_LOCALNET_MENU);//local battle
 					Menu.setMenuItems( DEF.LOCALNET_MENU_ITEMS_LIST);
	 			}
	 			break;
	 			
	 		case DEF.GS_SERVER_LIST_MENU:
	 			 if(Protocol.client_bt!=null)
	 			 {
	 				if(Protocol.client_bt.getBlueToothStatus()== BlueTooth.BTClient.SERVICES_COMPLETED_BLUETOOTH && firstTimeInit)
	 				{
	 					/// list all the server 
	 					int totalCount = Protocol.client_bt.getServerCount();
	 					String[] itemList = new String[totalCount];
	 					int k=0,i;
	 					for( i =0 ; i< totalCount; i++)
	 					{
	 						itemList[i] = Protocol.client_bt.getServerName(i);
//	 						if(s!=null)
//	 						{
//	 							itemList[k] = s;
//	 							k ++;
//	 						}
	 					}
	 					
//	 					String[] itemRealList = new String[k];
//	 					for(i =0; i < k; i++)
//	 						itemRealList[i] = itemList[i];
//	 					
	 					////
	 					firstTimeInit = false;
	 					Menu.setMenuItems( itemList);
	 				}
	 				//else
	 				//{
	 					
	 				//}
	 					
	 			 }
	 			if(Menu.getCurrentMenuCount()==0)
	 			return;
	 			//process press key
	 			if(GameInputKey.keyIsPressed(DEF.MOVE_UP))
	 			{
	 				if(Menu.getCurrentMenuIndex()== 0)
	 					Menu.setCurrentMenuIndex( Menu.getCurrentMenuCount() -1);
	 				else	
	 					Menu.setCurrentMenuIndex( (Menu.getCurrentMenuIndex() -1)% Menu.getCurrentMenuCount());
	 			}
	 			else if(GameInputKey.keyIsPressed(DEF.MOVE_DOWN))
	 			{
	 				Menu.setCurrentMenuIndex((Menu.getCurrentMenuIndex() +1)% Menu.getCurrentMenuCount());
	 			}

	 			else if(GameInputKey.keyIsPressed(DEF.SELECT))
	 			{
	 				///protocolProxy = new Protocol();
//	 				if(Menu.getCurrentMenuIndex()==0)
//	 				{
//			 			s_iGameModeState = 2;
//			 			m_gameEngine.m_aiGoBang.InitializeBoard();
//			 			//init protocol server
//			 			////protocolProxy.initProtocol(Protocol.BLUETOOTH_PROTOCOL, true);
//			 			s_sound.soundStop(); //stop the title music
//		 				setGameState(DEF.GS_RUN);  
//		 				s_iGameIsRun = 1;//start game
//		 				s_iGameOverState = 0;///not  game over 
//	 				}
//	 				else if(Menu.getCurrentMenuIndex()==1)
//	 				{
//	 					//use client socket
//			 			s_iGameModeState = 2;
//			 			m_gameEngine.m_aiGoBang.InitializeBoard();
//			 			///init protocol client
//			 			///protocolProxy.initProtocol(Protocol.BLUETOOTH_PROTOCOL, false);
//			 			s_sound.soundStop(); //stop the title music
//		 				setGameState(DEF.GS_RUN);
//		 				s_iGameIsRun = 1;//start game
//		 				s_iGameOverState = 0;///not  game over 
//	 				}
	 				
	 				/// selct the right server ...
	 				
	 				Protocol.client_bt.setProcessConnectionStatus(BlueTooth.BTClient.SUCCESS_CONNECTION);
	 				Protocol.client_bt.setCurrentRecordIndex(Menu.getCurrentMenuIndex());
	 				
	 				
	 				///Protocol.client_bt.initConnection();
	 				
	 				
	 				//	 //use client socket
			 			s_iGameModeState = 2;
			 			m_gameEngine.m_aiGoBang.InitializeBoard();
			 			///init protocol client
			 			///protocolProxy.initProtocol(Protocol.BLUETOOTH_PROTOCOL, false);
			 			s_sound.soundStop(); //stop the title music
		 				setGameState(DEF.GS_RUN);
		 				s_iGameIsRun = 1;//start game
		 				s_iGameOverState = 0;///not  game over 
		 				
	 				s_imageManage.setInGameBg(s_imageManage.createNewImage(DEF.IMAGE_BG_ITEMS_LIST[DEF.IN_GAME_BG_START_IMAGE]));
	 			}
	 			else if(GameInputKey.keyIsPressed(DEF.BACK))
	 			{

	 				protocolProxy.closeSocket_P2P();
 					setGameState(DEF.GS_BLUETOOTH_MENU);//bluetooth battle
 					Menu.setMenuItems( DEF.BLUETOOTH_MENU_ITEMS_LIST);
	 			}
	 			
	 			break;	
	 			
	 		case DEF.GS_LOCALNET_BATTLE:
//	 			if(GameInputKey.keyIsPressed(DEF.NUM1))
//	 			{
//	 				server = new SocketServer(DEFAULT_PORT);
//	 				server.start();
//	 				setGameState(DEF.GS_RUN);
//	 			}
//	 			else if(GameInputKey.keyIsPressed(DEF.NUM3)) 
//	 			{
//	 				client = new SocketClient(DEFAULT_PORT);
//	 				client.start();
//	 				setGameState(DEF.GS_RUN);
//	 			}
//	 			s_iGameModeState = 1;
	 			// This is for test TCP protocol
	 			//process press key
	 			if(GameInputKey.keyIsPressed(DEF.MOVE_UP))
	 			{
	 				if(Menu.getCurrentMenuIndex()== 0)
	 					Menu.setCurrentMenuIndex(Menu.getCurrentMenuCount() -1);
	 				else	
	 					Menu.setCurrentMenuIndex( (Menu.getCurrentMenuIndex() -1)% Menu.getCurrentMenuCount());
	 			}
	 			else if(GameInputKey.keyIsPressed(DEF.MOVE_DOWN))
	 			{
	 				Menu.setCurrentMenuIndex((Menu.getCurrentMenuIndex() +1)% Menu.getCurrentMenuCount());
	 			}

	 			else if(GameInputKey.keyIsPressed(DEF.SELECT))
	 			{
	 				if(Menu.getCurrentMenuIndex()==0)
	 				{
			 			s_iGameModeState = 1;
			 			m_gameEngine.m_aiGoBang.InitializeBoard();
	 					//use server socket
			 			protocolProxy.initProtocol(Protocol.TCP_PROTOCOL, true);
			 			s_sound.soundStop(); //stop the title music
		 				setGameState(DEF.GS_RUN);  
		 				s_iGameIsRun = 1;//start game
		 				s_iGameOverState = 0;///not  game over 
	 				}
	 				else if(Menu.getCurrentMenuIndex()==1)
	 				{
	 					//use client socket
			 			s_iGameModeState = 1;
			 			m_gameEngine.m_aiGoBang.InitializeBoard();
			 			protocolProxy.initProtocol(Protocol.TCP_PROTOCOL, false);
			 			s_sound.soundStop(); //stop the title music
		 				setGameState(DEF.GS_RUN);
		 				s_iGameIsRun = 1;//start game
		 				s_iGameOverState = 0;///not  game over 
	 				}
	 				
	 				s_imageManage.setInGameBg(s_imageManage.createNewImage(DEF.IMAGE_BG_ITEMS_LIST[DEF.IN_GAME_BG_START_IMAGE]));
	 			}
	 			else if(GameInputKey.keyIsPressed(DEF.BACK))
	 			{
 					setGameState(DEF.GS_LOCALNET_MENU);//local battle
 					Menu.setMenuItems( DEF.LOCALNET_MENU_ITEMS_LIST);
	 			}
	 			break;
	 		case DEF.GS_LOAD:
	 			
	 			break;
	 		case DEF.GS_RUN:
	 		 if(s_iGameIsRun!=0)
	 		 {
	 			 
	 			// play the win&lose music
	 			if(m_gameEngine.m_aiGoBang.playOnceMusic)
	 			{
	 				Menu.setMenuItems(DEF.GAME_OVER_MENU_ITEMS_LIST);
	 			if(m_gameEngine.m_aiGoBang.cwin )
	 			    s_sound.soundPlay(DEF.M_GAME_LOSE_SOUND); //person lose
	 			else if( m_gameEngine.m_aiGoBang.pwin)
	 				s_sound.soundPlay(DEF.M_GAME_WIN_SOUND);
	 			
	 			m_gameEngine.m_aiGoBang.playOnceMusic =false;
	 			}
		 			
	 			switch(s_iGameModeState )
	 			{
		 			case 0:
		 			
		 			ChessBoard.UpdateCursor();
		 			m_gameEngine.m_aiGoBang.updateAi();
	 				//wait 20 frames go back to MM
		 			if((m_gameEngine.m_aiGoBang.cwin || m_gameEngine.m_aiGoBang.pwin) && m_gameEngine.s_gameFrame%100 ==0)
		 			{
		 				s_iGameOverState = 1;// add the delay time
		 			}
		 			if(s_iGameOverState ==1)
		 			{
//	 				if(m_gameEngine.s_gameFrame%20 ==0)
//	 				{
//	 		 			setGameState(DEF.GS_MAIN_MENU);
//	 		 			Menu.setMenuItems( DEF.MAIN_MENU_ITEMS_LIST);// return to MM
//	 				}
		 				
		 				
		 				 // game over screen
			 			//process press key
				 			if(GameInputKey.keyIsPressed(DEF.MOVE_UP))
				 			{
				 				if(Menu.getCurrentMenuIndex()== 0)
				 					Menu.setCurrentMenuIndex( Menu.getCurrentMenuCount() -1);
				 				else	
				 					Menu.setCurrentMenuIndex( (Menu.getCurrentMenuIndex() -1)% Menu.getCurrentMenuCount());
				 			}
				 			else if(GameInputKey.keyIsPressed(DEF.MOVE_DOWN))
				 			{
				 				Menu.setCurrentMenuIndex((Menu.getCurrentMenuIndex() +1)% Menu.getCurrentMenuCount());
				 			}

				 			else if(GameInputKey.keyIsPressed(DEF.SELECT))
				 			{
				 				if(Menu.getCurrentMenuIndex()==0)
				 				{
					 				m_gameEngine.m_aiGoBang.setAiGrade(s_gradeId);
					 				setGameState(DEF.GS_NORMAL_BATTLE);//new game
					 				
					 				s_iGameOverState = 0;
				 				}
				 				else if(Menu.getCurrentMenuIndex()==1)
				 				{
				 					if(s_iGameModeState == 1 || s_iGameModeState==2)
					 					//close the socket &send thread
				 						protocolProxy.closeSocket_P2P();
					 					///
				 					s_iGameOverState = 0;
				 					s_sound.soundPlay(DEF.M_TITLE_SOUND);///
				 		 			setGameState(DEF.GS_MAIN_MENU);
				 		 			Menu.setMenuItems( DEF.MAIN_MENU_ITEMS_LIST);// return to MM
				 				}
				 			}
		 				
		 			}
		 			
		 			break;
		 			
		 			case 1: ///1:local network Human&human
		 			case 2:	 // 2:buletooth Human&Human; 
//		 				if(GameInputKey.keyIsPressed(DEF.NUM1))
//		 					server.sender.send("come from Server");
//		 				else if(GameInputKey.keyIsPressed(DEF.NUM3))
//		 					client.sender.send("come from Client^-^");
		 				
		 				ChessBoard.UpdateCursor();
		 				m_gameEngine.m_aiGoBang.updateAi_P2P();
		 				
		 				//wait 20 frames go back to MM
			 			if(m_gameEngine.m_aiGoBang.cwin || m_gameEngine.m_aiGoBang.pwin)
		 				if(m_gameEngine.s_gameFrame%20 ==0)
		 				{
		 					//close the socket &send thread
		 					protocolProxy.closeSocket_P2P();
		 					///
		 					GameView.s_debug_String = " ";//reset
		 		 			setGameState(DEF.GS_MAIN_MENU);
		 		 			Menu.setMenuItems( DEF.MAIN_MENU_ITEMS_LIST);// return to MM
		 				}
		 				else if(Protocol.client_bt!=null && Protocol.client_bt.getBlueToothStatus()== BlueTooth.BTClient.EXCEPTION_EXIT_BLUETOOTH)
		 				{
		 					///happen exception
		 					
		 					if(m_gameEngine.s_gameFrame%200 ==0)
			 				{
			 					//close the socket &send thread
			 					protocolProxy.closeSocket_P2P();
			 					///
			 					GameView.s_debug_String = " ";//reset
			 		 			setGameState(DEF.GS_MAIN_MENU);
			 		 			Menu.setMenuItems( DEF.MAIN_MENU_ITEMS_LIST);// return to MM
			 				}
		 				}
		 				break;
//		 			case 2:	 // 2:buletooth Human&Human; 
//		 				break;
		 			case 3:	///3:internet Human&Human
		 				break;
	 			}
	 			
	 			
	 			//callback in game menu
	 		 if(GameInputKey.keyIsPressed(DEF.BACK))
	 			{
	 			s_iGameIsRun = 0 ;
	 			GameView.s_bRefreshAll = true;
	 			Menu.setMenuItems(DEF.INGAME_MENU_ITEMS_LIST);
	 			}
	 		 } 
	 		 else
	 		 {
	 			 //pause game
	 			//process press key
		 			if(GameInputKey.keyIsPressed(DEF.MOVE_UP))
		 			{
		 				if(Menu.getCurrentMenuIndex()== 0)
		 					Menu.setCurrentMenuIndex( Menu.getCurrentMenuCount() -1);
		 				else	
		 					Menu.setCurrentMenuIndex( (Menu.getCurrentMenuIndex() -1)% Menu.getCurrentMenuCount());
		 			}
		 			else if(GameInputKey.keyIsPressed(DEF.MOVE_DOWN))
		 			{
		 				Menu.setCurrentMenuIndex((Menu.getCurrentMenuIndex() +1)% Menu.getCurrentMenuCount());
		 			}

		 			else if(GameInputKey.keyIsPressed(DEF.SELECT))
		 			{
		 				if(Menu.getCurrentMenuIndex()==0)
		 				{
		 					s_iGameIsRun = 1;//resume game
		 				}
		 				else if(Menu.getCurrentMenuIndex()==1)
		 				{
		 					if(s_iGameModeState == 1 || s_iGameModeState==2)
			 					//close the socket &send thread
		 						protocolProxy.closeSocket_P2P();
			 					///
		 					GameView.s_debug_String = " ";//reset
		 					s_sound.soundPlay(DEF.M_TITLE_SOUND);///
		 		 			setGameState(DEF.GS_MAIN_MENU);
		 		 			Menu.setMenuItems( DEF.MAIN_MENU_ITEMS_LIST);// return to MM
		 				}
		 				else if(Menu.getCurrentMenuIndex()==2)
		 				{
		 					if(s_iGameModeState == 1 || s_iGameModeState==2)
		 					//close the socket &send thread
		 						protocolProxy.closeSocket_P2P();
		 					///
		 					GameEngine.stoped = true;;// Exit game
		 				}
		 			}
		 			else if(GameInputKey.keyIsPressed(DEF.BACK))
		 			{
		 				s_iGameIsRun = 1;//resume game
		 			}
	 		 }
	 			break;
	 		case DEF.GS_ABOUT:
	 		  if(GameInputKey.keyIsPressed(DEF.BACK))
	 			{
		 			setGameState(DEF.GS_MAIN_MENU);
		 			Menu.setMenuItems( DEF.MAIN_MENU_ITEMS_LIST);
	 			}
	 			break;	
	 		case DEF.GS_HELP_MENU :
		 		  if(GameInputKey.keyIsPressed(DEF.BACK))
		 			{
			 			setGameState(DEF.GS_MAIN_MENU);
			 			Menu.setMenuItems( DEF.MAIN_MENU_ITEMS_LIST);
		 			}
		 			break;
	 		default:
	 			break;
	 		}
	 	}
	 	catch (Exception e)
	 	{
	 		e.printStackTrace();
	 		System.out.println(e.toString());
	 	}
   }
   
   public void setGameState(int gameState)
   {
	   s_iGameState = gameState;
	   GameView.s_bRefreshAll = true;
   }
   public int getGameState()
   {
	   return s_iGameState;
   }
}